<?php 
class Logger { 
	private $mySqlConnector;
	public function __construct($_mySqlConnector) {
		$this->mySqlConnector = $_mySqlConnector;
	}
	
	public function log($command) { 
		$isConnected = $this->mySqlConnector->isConnected();
		$this->mySqlConnector->connect();
		$countOfCommand = $this->getCountOfCommand($command);
		if($countOfCommand==0){
			$query = "INSERT INTO `statistik`(`command`, `count`) VALUES ('".$command."', 1)";
		} else {
			$query = "UPDATE `statistik` SET `count`='".($countOfCommand+1)."' WHERE command = '".$command."'";
		}
		mysql_query($query) or die($query."<br/><br/>".mysql_error());
		if(!$isConnected){
			$this->mySqlConnector->disconnect();
		}
	} 
	
	private function isCommandExist($command){
		$lectureName = mysql_real_escape_string($lectureName);  // SECURITY!
		$result = mysql_query("SELECT 1 FROM preditors_assigned WHERE lecture_name='$lectureName' LIMIT 1");
		if (mysql_fetch_row($result)) {
			return true;
		} else {
			return false;
		}
	}
	
	private function getCountOfCommand($command){
		$query = "SELECT `count` FROM `statistik` WHERE command = '".$command."'";
		$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
		if (mysql_num_rows($result) > 0) {
			$count = mysql_result($result, 0, "count");
		} else {
			$count = 0;
		}
		return $count;
	}
} 